<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../../forms/resources/common.js"></script>
<script src="../../forms/resources/picker-common.js"></script>
<script src="resources/calendar-picker-common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<input type=week id=week value="2000-W01">
<script>
description("Tests if calendar picker mouse operations work as expected.");

debug('Check that page popup doesn\'t exist at first.');
shouldBeNull('document.getElementById("mock-page-popup")');

openPicker(document.getElementById('week'), test);

var position;
eventSender.clearTouchPoints();

function test() {
    debug('Open the month popup.');
    shouldBeFalse('popupWindow.global.picker.monthPopupView.isVisible');
    clickMonthPopupButton();
    shouldBeTrue('popupWindow.global.picker.monthPopupView.isVisible');

    debug('Check that touch swiping scrolls the list in the month popup.');

    checkYearListViewScrollOffset();

    popupWindow.global.picker.monthPopupView.yearListView.element.addEventListener('touchend', testAfterSwipingDown, false);

    position = skipAnimationAndGetPositionOfMonthButton(2000, 0);
    eventSender.addTouchPoint(position.x, position.y);
    eventSender.touchStart();
    position = skipAnimationAndGetPositionOfMonthButton(2000, 11);
    eventSender.updateTouchPoint(0, position.x, position.y);
    eventSender.touchMove();
    eventSender.releaseTouchPoint(0);
    eventSender.touchEnd();
}

function testAfterSwipingDown() {
    shouldBeTrue('checkYearListViewScrollOffset() > 0');
    popupWindow.global.picker.monthPopupView.yearListView.element.removeEventListener('touchend', testAfterSwipingDown, false);
    setTimeout(testSwipingUp, 0);
}

function testSwipingUp() {
    checkYearListViewScrollOffset();
    popupWindow.global.picker.monthPopupView.yearListView.element.addEventListener('touchend', testAfterSwipingUp, false);

    eventSender.addTouchPoint(position.x, position.y);
    eventSender.touchStart();
    position = skipAnimationAndGetPositionOfMonthPopupButton();
    eventSender.updateTouchPoint(0, position.x, position.y);
    eventSender.touchMove();
    eventSender.releaseTouchPoint(0);
    eventSender.touchEnd();
}

function testAfterSwipingUp() {
    shouldBeTrue('checkYearListViewScrollOffset() < 0');
    popupWindow.global.picker.monthPopupView.yearListView.element.removeEventListener('touchend', testAfterSwipingUp, false);
    finishJSTest();
}
</script>
</body>
</html>
